/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.Customer;
import ejb.CustomerFacade;
import ejb.CustomerOrder;
import ejb.CustomerOrderFacade;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author RasJLK
 */
@WebServlet(name = "SaveOrder", urlPatterns = {"/SaveOrder"})
public class SaveOrder extends HttpServlet {

    @EJB
    private CustomerFacade customerFacade;
    @EJB
    private CustomerOrderFacade customerOrderFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String orderNo = request.getParameter("orderNo");
        String dueDate = request.getParameter("dueDate");
        String comments = request.getParameter("comments");
        String ammount = request.getParameter("ammount");
        String custId = request.getParameter("customerId");
        String orderId = request.getParameter("id");

        Customer c = customerFacade.getCustomerById(Long.parseLong(custId));
        CustomerOrder cOder = new CustomerOrder();
        cOder.setAmmount(Double.parseDouble(ammount));
        cOder.setComments(comments);
        cOder.setCustomer(c);
        cOder.setOrderNo(orderNo);
        try {
            
            cOder.setDueDate(DateFormat.getDateInstance(DateFormat.SHORT).parse(dueDate));
        } catch (ParseException ex) {
            Logger.getLogger(SaveOrder.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (orderId != null && !orderId.equals("null")) {
            cOder.setId(Long.parseLong(orderId));
            customerOrderFacade.edit(cOder);
        } else {
            customerOrderFacade.create(cOder);
        }
        response.sendRedirect("OrderList");



    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
